#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
using namespace std;

void SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFD);
    if(fl == -1)
    {
        cout << errno << ": " << strerror(errno) << endl;
    }

    int ret = fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}

int main()
{

    SetNonBlock(0);
    while(true)
    {
        char buffer[1024];
        ssize_t n = read(0, buffer, sizeof(buffer) - 1);
        if(n > 0)
        {
            cout << buffer << endl;
        }
        else if(n == 0)
        {
            cout << "数据读取完毕" << endl;
            break;
        }
        else
        {
            cout << "stdin error" << endl;
            // return 1;
        }
        sleep(1);
    }
    return 0;
}